package net.hzlj.platform.enterprise.utils.recorder;

import android.media.MediaRecorder;

import java.io.File;
import java.util.UUID;

/**
 * @ClassName: RecorderAudioManager
 * @Description: 录音管理
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2016-01-11 15:45:09
 */
public class RecorderAudioManager implements RecorderAudioService {

    public interface RecordingType{
        int AMR = 100;
        int WAV = 101;
        int WAV_OM = 102;
        int MP3 = 103;
    }

    private static RecorderAudioManager mInstance;

    //录音格式（现在只支持amr,wav）
    private static int mRecordingType =  RecordingType.AMR;

    private RecorderAudioService recorderAudioService = new AmrRecorderAudioImpl();


    /**
     * 获取RecorderAudioManager对象
     *
     * @return
     */
    public static RecorderAudioManager getInstance(int type) {
        if (null == mInstance) {
            synchronized (RecorderAudioManager.class) {
                if (null == mInstance) {
                    mInstance = new RecorderAudioManager(type);
                }
            }
        }
        return mInstance;
    }


    private RecorderAudioManager(int type) {
        if (type == RecordingType.AMR){
            recorderAudioService = new AmrRecorderAudioImpl();
        } else if (type == RecordingType.WAV ){
            recorderAudioService = new WavRecorderAudioImpl();
        } else if (type == RecordingType.WAV_OM){
            recorderAudioService = new WavOmRecorderAudioImpl();
        } else if (type == RecordingType.MP3){
            recorderAudioService = new Mp3RecorderAudioImpl();
        }
    }

    /**
     * 设置录音准备完毕时的监听事件
     *
     * @param listener
     */
    public void setOnAudioStateListener(AudioStateListener listener) {
        recorderAudioService.setOnAudioStateListener(listener);
    }

    @Override
    public void setRecorderFileDir(File dir) {
        recorderAudioService.setRecorderFileDir(dir);
    }


    /**
     * 准备
     */
    public void prepareAudio() {
        recorderAudioService.prepareAudio();
    }

    /**
     * 获取音量等级
     *
     * @return
     */
    public int getVoiceLevel(int maxLevel) {
        return recorderAudioService.getVoiceLevel(maxLevel);
    }

    public void release() {
        recorderAudioService.release();
    }

    public void cancel() {
        recorderAudioService.cancel();
    }

    /**
     * 获取当前录音文件路径
     *
     * @return
     */
    public String getCurrentFilePath() {
        return recorderAudioService.getCurrentFilePath();
    }

    /**
     * 录音准备完毕时监听事件
     *
     * @author async
     *
     */
    public interface AudioStateListener {
        /**
         * 录音准备完毕
         */
        public void wellPrepared();
    }
}
